﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class supports the development of complex Taylor series out of complex polynomials.
    /// </summary>
    [Serializable]
    public class TaylorComplexSeriesDevelopment
    {
        /// <summary>
        /// The base polynomial for developing the Taylor series.
        /// </summary>
        private readonly ComplexPolynomial basePolynomial;

        /// <summary>
        /// Initializes a new instance of the <see cref="TaylorComplexSeriesDevelopment"/> class.
        /// </summary>
        /// <param name="polynomial">The base polynomial for developing the Taylor series.</param>
        public TaylorComplexSeriesDevelopment(ComplexPolynomial polynomial)
        {
            if (polynomial == (ComplexPolynomial) null)
            {
                throw new ArgumentNullException("polynomial");
            }

            this.basePolynomial = polynomial;
        }

        /// <summary>
        /// Develops the Taylor series.
        /// </summary>
        /// <param name="a">The function about point of the series.</param>
        /// <param name="n">The maximum derivative, which should be used to develop the series.</param>
        /// <returns>The developed Taylor series.</returns>
        public TaylorComplexSeries DevelopSeries(ComplexNumber a, int n)
        {
            TaylorComplexSeries result = new TaylorComplexSeries();
            ComplexPolynomial tempuri = this.basePolynomial.Copy();

            result.ConstantValue = this.basePolynomial.SolveAt(a);

            for (int i = 1; i <= n; i++)
            {
                tempuri = tempuri.Derivative();

                ComplexNumber coefficient = tempuri.SolveAt(a) / Factorials.Factorial((uint) i);

                result.Components.Add(new TaylorComplexSeriesComponent(coefficient, i, a));
            }

            return result;
        }
    }
}